/*
 * File      : filerw.c
 * Change Logs:
 * Date 		Author			Notes
 * 20130710		jimmy.lee		first version
 */
#include "filerw.h"

#ifdef RT_USING_DFS_FILERW

static int stdio_seek(struct rt_filerw *fp, rt_off_t offset, int whence)
{
    int stdio_whence[3] = {SEEK_SET, SEEK_CUR, SEEK_END};

    if (whence < RT_FILE_SEEK_SET || whence > RT_FILE_SEEK_END)
    {
        return -1;
    }

    return lseek(fp->fd, offset, stdio_whence[whence]);
}

static int stdio_read(struct rt_filerw *fp, void *ptr, rt_size_t size, rt_size_t maxnum)
{
    int result;

    /* end of file */
    if (fp->eof == RT_TRUE) return -1;

    result = read(fp->fd, ptr, size * maxnum);
    if (result == 0) fp->eof = RT_TRUE;

    return result;
}

static int stdio_write(struct rt_filerw *fp, const void *ptr, rt_size_t size, rt_size_t num)
{
    return write(fp->fd, (char *)ptr, size * num);
}

static int stdio_tell(struct rt_filerw *fp)
{
    return lseek(fp->fd, 0, SEEK_CUR);
}

static int stdio_eof(struct rt_filerw *fp)
{
    int result;

    if (fp->eof == RT_TRUE) result = 1;
    else result = -1;

    return result;
}

static int stdio_close(struct rt_filerw *fp)
{

    if (fp)
    {
        close(fp->fd);
        rt_free(fp);

        return 0;
    }

    return -1;
}

#endif



static int mem_seek(struct rt_filerw *fp, rt_off_t offset, int whence)
{
    const rt_uint8_t *newpos;

    RT_ASSERT(fp != RT_NULL);

    switch (whence)
    {
    case RT_FILE_SEEK_SET:
        newpos = fp->mem_base + offset;
        break;

    case RT_FILE_SEEK_CUR:
        newpos = fp->mem_position + offset;
        break;

    case RT_FILE_SEEK_END:
        newpos = fp->mem_end + offset;
        break;

    default:
        return -1;
    }

    if (newpos < fp->mem_base)
        newpos = fp->mem_base;

    if (newpos > fp->mem_end)
        newpos = fp->mem_end;

    fp->mem_position = newpos;
    return fp->mem_position - fp->mem_base;
}

static int mem_read(struct rt_filerw *fp, void *ptr, rt_size_t size, rt_size_t maxnum)
{
    int total_bytes;
    int mem_available;
    total_bytes = (maxnum * size);
    if ((maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size))
    {
        return -1;
    }

    mem_available = fp->mem_end - fp->mem_position;
    if (total_bytes > mem_available)
        total_bytes = mem_available;

    rt_memcpy(ptr, fp->mem_position, total_bytes);
    fp->mem_position += total_bytes;

    return (total_bytes / size);
}

static int mem_write(struct rt_filerw *fp, const void *ptr, rt_size_t size, rt_size_t num)
{
    return 0; /* not support memory write */
}

static int mem_tell(struct rt_filerw *fp)
{
    return fp->mem_position - fp->mem_base;
}

static int mem_eof(struct rt_filerw *fp)
{
    return fp->mem_position >= fp->mem_end;
}

static int mem_close(struct rt_filerw *fp)
{

    if (fp != RT_NULL)
    {
        rt_free(fp);
        return 0;
    }

    return -1;
}

const rt_uint8_t *rt_filerw_mem_getdata(struct rt_filerw *fp)
{
    return fp->mem_base;
}

/* file read/write public interface */
#ifdef RT_USING_DFS_FILERW
static int parse_mode(const char *mode)
{
    int f = 0;

    for (;;)
    {
        switch (*mode)
        {
        case 0:
            return f;
        case 'b':
            f |= O_BINARY;
            break;
        case 'r':
            f = O_RDONLY;
            break;
        case 'w':
            f = O_WRONLY | O_CREAT | O_TRUNC;
            break;
        case 'a':
            f = O_WRONLY | O_CREAT | O_APPEND;
            break;
        case '+':
            f = (f & (~O_WRONLY)) | O_RDWR;
            break;
        }

        ++mode;
    }
}

struct rt_filerw *stdio_open(const char *filename, const char *mode)
{
    int fd;
    struct rt_filerw *fp;

    RT_ASSERT(filename != RT_NULL);

    fp = RT_NULL;
#ifdef _WIN32_NATIVE
    fd = _open(filename, parse_mode(mode), 0);
#else
    fd = open(filename, parse_mode(mode), 0);
#endif

    if (fd >= 0)
    {
        fp = (struct rt_filerw *) rt_malloc(sizeof(struct rt_filerw));
        if (fp != RT_NULL)
        {
//             fp->parent.seek  = stdio_seek;
//             fp->parent.read  = stdio_read;
//             fp->parent.write = stdio_write;
//             fp->parent.tell  = stdio_tell;
//             fp->parent.close = stdio_close;
//             fp->parent.eof   = stdio_eof;

            fp->fd  = fd;
            fp->eof = RT_FALSE;
        }
    }

    return fp;
}

int rt_filerw_unlink(const char *filename)
{
#ifdef _WIN32_NATIVE
    return _unlink(filename);
#else
#ifndef RT_USING_DFS
    /* no unlink function */
    return -1;
#else
    return unlink(filename);
#endif
#endif
}

#endif

struct rt_filerw *mem_create(const rt_uint8_t *mem, rt_size_t size)
{
    struct rt_filerw *fp;
    RT_ASSERT(mem != RT_NULL);

    fp = (struct rt_filerw *) rt_malloc(sizeof(struct rt_filerw));
    if (fp != RT_NULL)
    {
//         fp->parent.seek  = mem_seek;
//         fp->parent.read  = mem_read;
//         fp->parent.write = mem_write;
//         fp->parent.tell  = mem_tell;
//         fp->parent.eof   = mem_eof;
//         fp->parent.close = mem_close;

        fp->mem_base = mem;
        fp->mem_position = mem;
        fp->mem_end = mem + size;
    }

    return fp;
}

int rt_filerw_seek(struct rt_filerw *fp, rt_off_t offset, int whence)
{
    RT_ASSERT(fp != RT_NULL);

    return stdio_seek(fp, offset, whence);
}

int rt_filerw_read(struct rt_filerw *fp, void *buffer, rt_size_t size, rt_size_t count)
{
    RT_ASSERT(fp != RT_NULL);

    return stdio_read(fp, buffer, size, count);
}

int rt_filerw_write(struct rt_filerw *fp, const void *buffer, rt_size_t size, rt_size_t count)
{
    RT_ASSERT(fp != RT_NULL);

    return stdio_write(fp, buffer, size, count);
}

int rt_filerw_eof(struct rt_filerw *fp)
{
    RT_ASSERT(fp != RT_NULL);

    return stdio_eof(fp);
}

int rt_filerw_tell(struct rt_filerw *fp)
{
    RT_ASSERT(fp != RT_NULL);

    return stdio_tell(fp);
}

int rt_filerw_close(struct rt_filerw *fp)
{
    int result;

    RT_ASSERT(fp != RT_NULL);

    /* close fp */
    result = stdio_close(fp);
    if (result != 0)
    {
        /* close file failed */
        return -1;
    }

    return 0;
}

